package com.bjgzy.rating.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.rating.R;
import com.bjgzy.rating.R2;
import com.bjgzy.rating.di.component.DaggerSignUpComponent;
import com.bjgzy.rating.mvp.adapter.ExpandableItemAdapter;
import com.bjgzy.rating.mvp.contract.SignUpContract;
import com.bjgzy.rating.mvp.presenter.SignUpPresenter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.EntriesDetailData;
import com.eduhzy.ttw.commonsdk.entity.RatingHomeData;
import com.eduhzy.ttw.commonsdk.entity.SaveWorksData;
import com.eduhzy.ttw.commonsdk.entity.SignUp1ParentData;
import com.eduhzy.ttw.commonsdk.entity.SignUp1SonData;
import com.eduhzy.ttw.commonsdk.entity.SignUp2Data;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-9 11:43:59
 * <p>
 * 活动评比报名信息第一页 选择分组
 */
@Route(path = RouterHub.RATING_SIGNUP1ACTIVITY)
public class SignUp1Activity extends BaseActivity<SignUpPresenter> implements SignUpContract.View {

    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private SaveWorksData saveData;
    @Autowired(name = Constants.ROUTER_DATA)
    RatingHomeData data;
    @Autowired(name = Constants.PARCELABLE_DATA)
    EntriesDetailData mEntriesDetailData;
    @Inject
    ExpandableItemAdapter mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    List<MultiItemEntity> lists;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerSignUpComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.rating_activity_sign_up1;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        swipeLayout.setOnRefreshListener(() -> mPresenter.getClassify(data.getId()));
        saveData = new SaveWorksData();
        rvList.setAdapter(mAdapter);
        rvList.setLayoutManager(layoutManager);
        mPresenter.getClassify(data.getId());

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            MultiItemEntity entity = lists.get(position);
            if (entity.getItemType() == 0) {
                SignUp1ParentData lv0 = (SignUp1ParentData) entity;
                if (lv0.getSonNum() > 0) {
                    if (lv0.isExpanded()) {
                        mAdapter.collapse(position);
                    } else {
                        mAdapter.expand(position);
                    }
                    return;
                } else {
                    RxUtil.showConfirmDialog(getActivity(), "提示", "确定选择\"" + lv0.getClassifyName() + "\"该分组吗", (dialog, index) -> {
                        dialog.dismiss();
                        saveData.setcId(lv0.getId());
                        next();
                    });
                }
            } else {
                SignUp1SonData lv1 = (SignUp1SonData) entity;
                RxUtil.showConfirmDialog(getActivity(), "提示", "确定选择\"" + lv1.getClassifyName() + "\"该分组吗", (dialog, index) -> {
                    dialog.dismiss();
                    saveData.setcId(lv1.getId());
                    next();
                });
            }
        });
    }

    private void next() {
        ARouter.getInstance()
                .build(RouterHub.RATING_SIGNUP2ACTIVITY)

                .withParcelable(Constants.ROUTER_DATA, data)
                .withParcelable(Constants.PARCELABLE_DATA,mEntriesDetailData)
                .withInt(Constants.ROUTER_INTEGER, saveData.getcId())
                .withString(Intent.EXTRA_TITLE, Constants.SIGNUP)
                .navigation();
        killMyself();
    }

    @Override
    public void showLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void update(SignUp2Data data) {

    }


    @OnClick(R2.id.btn_next)
    public void onViewClicked() {

    }

}
